package com.bigtotoro.shuiyincamera.video;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.bigtotoro.shuiyincamera.MyArtListActivity;
import com.bigtotoro.shuiyincamera.MyArtsActivity;
import com.bigtotoro.shuiyincamera.R;
import com.bigtotoro.shuiyincamera.video.trim.VideoTrimmerActivity;
import com.bigtotoro.shuiyincamera.util.VideoUtil;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.picture.lib.tools.PictureFileUtils;
import com.umeng.analytics.MobclickAgent;

import java.util.List;

//import VideoHandle.EpEditor;
//import VideoHandle.OnEditorListener;
import qiu.niorgai.StatusBarCompat;

/**
 * 视频添加水印界面
 */
public class VideoAddWaterActivity extends AppCompatActivity implements View.OnClickListener {
    public static final String TAG = "VideoAddWaterActivity";
    private ProgressDialog mProgressDialog;
    private TextView textView;
    String path;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_add_water);
        StatusBarCompat.setStatusBarColor(this, getResources().getColor(R.color.font_0));

        textView = findViewById(R.id.textView);

        findViewById(R.id.btn_pick).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                PictureSelector.create(VideoAddWaterActivity.this).openGallery(PictureMimeType.ofVideo())
                        .previewVideo(true).selectionMode(PictureConfig.SINGLE).videoMinSecond(1).forResult(PictureConfig.CHOOSE_REQUEST);
            }
        });
        findViewById(R.id.btn_run).setOnClickListener(this);
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case PictureConfig.CHOOSE_REQUEST:
                    // 图片、视频、音频选择结果回调
                    List<LocalMedia> selectList = PictureSelector.obtainMultipleResult(data);
                    if(selectList.size()>0){
                        Toast.makeText(this, ""+selectList.get(0).getPath(), Toast.LENGTH_SHORT).show();
                        path = selectList.get(0).getPath();
                    }

                    startActivity(new Intent(this, VideoTrimmerActivity.class)
                        .putExtra(VideoTrimmerActivity.VIDEO_PATH_KEY, path));
                    // 例如 LocalMedia 里面返回三种path
                    // 1.media.getPath(); 为原图path
                    // 2.media.getCutPath();为裁剪后path，需判断media.isCut();是否为true  注意：音视频除外
                    // 3.media.getCompressPath();为压缩后path，需判断media.isCompressed();是否为true  注意：音视频除外
                    // 如果裁剪并压缩了，以取压缩路径为准，因为是先裁剪后压缩的
//                    adapter.setList(selectList);
//                    adapter.notifyDataSetChanged();
                    break;
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        PictureFileUtils.deleteCacheDirFile(this);
    }


    @Override
    public void onClick(View view) {
        if(view.getId()==R.id.btn_run){
//            EpVideo
            mProgressDialog = new ProgressDialog(this);
            mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            mProgressDialog.setMax(100);
            mProgressDialog.setCancelable(false);
            mProgressDialog.setCanceledOnTouchOutside(false);
            mProgressDialog.setTitle("正在处理");

            mProgressDialog.show();
            //去水印ffmpeg -i logo.mp4 -filter_complex "delogo=x=100:y=100:w=100:h=100:show=1" delogo.mp4
            String out = VideoUtil.VIDEO_SLIDE_DIR+"/"+System.currentTimeMillis()+".mp4";
            String cmd = String.format("-i %s -filter_complex delogo=x=100:y=100:w=100:h=100:show=1 %s", path, out);
            int duration = VideoUtil.getDuration(this, path);
            Log.d(TAG, "duration "+duration);
            //FFmpeg修改
//            EpEditor.execCmd(cmd, duration*1000, new OnEditorListener() {
//                @Override
//                public void onSuccess() {
//                    mHandler.sendEmptyMessage(MSG_SUCCESS);
//                }
//
//                @Override
//                public void onFailure() {
//                    mHandler.sendEmptyMessage(MSG_FAIL);
//                }
//
//                @Override
//                public void onProgress(float progress) {
//                    Message msg = new Message();
//                    Bundle data = new Bundle();
//                    data.putFloat("progress", progress);
//                    msg.setData(data);
//                    msg.what = MSG_PROGRESS;
//                    mHandler.sendMessage(msg);
////                    mProgressDialog.setProgress((int) (progress * 100));
//                }
//            });
        }
    }

    public static final int MSG_SUCCESS = 101;
    public static final int MSG_FAIL = 102;
    public static final int MSG_PROGRESS = 103;
    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            if(msg.what==MSG_SUCCESS){
                Toast.makeText(VideoAddWaterActivity.this, "处理完成", Toast.LENGTH_SHORT).show();
                if(mProgressDialog.isShowing()){
                    mProgressDialog.dismiss();
                }
                startActivity(new Intent(VideoAddWaterActivity.this, MyArtListActivity.class));
            }else if(msg.what==MSG_FAIL){
                Toast.makeText(VideoAddWaterActivity.this, "处理失败", Toast.LENGTH_SHORT).show();
                if(mProgressDialog.isShowing()){
                    mProgressDialog.dismiss();
                }
            }else if(msg.what==MSG_PROGRESS){
                float progress = msg.getData().getFloat("progress");
                mProgressDialog.setProgress((int) (progress));
            }
        }
    };

    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("VideoAddWaterActivity");//统计页面
        MobclickAgent.onResume(this);
    }
    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("VideoAddWaterActivity");//统计页面
        MobclickAgent.onPause(this);
    }
}
